package paint;

import java.awt.Toolkit.*;
import java.awt.Image.*;
import java.util.*;
import java.awt.datatransfer.Clipboard.*;
import java.awt.*;
import java.awt.Toolkit.*;
import java.awt.image.*;

/** A blur object is a dialogue box that allows the user to blur the whole image.
 * The user can then edit the options in the dialog box, and apply the changes to the image.
 * Dialog box is terminated by clicking on the 'OK' or 'Cancel' buttons. OK will instantiated the changes,
 * Cancel will do nothing to the original image.
 * The user can change the times of blur to any integer between 1 and 50.
 * There are no OS/Hardware dependencies and no variances.  There is no need for any
 * security constraints and no references to external specifications.
 *
 * @author  Paint
 * @version 2.0
 */
public class blur extends javax.swing.JDialog {

    /** Creates new form blur.
     *
     * This also initializes all the components and calls the parent's default
     * constructor.
     * There are no OS dependencies and variances.  No security constraints or external
     * specifications.
     * @param parent This parameter sends which frame is the parent.
     * @param modal This parameter could be a model, true or false.
     */
    public blur(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    /*FAULT::ok.setText("poop");*/
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     *
     * This method initializes the entire form with buttons and new dialoge
     * boxes, etc.  It also sets up constraints.
     * There are no OS dependencies and variances.  No security constraints or external
     * specifications.
     */
    public void initComponents() {
        //	  ok_action = false;
        ok_cancel = new javax.swing.JPanel();
        ok = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        center = new javax.swing.JPanel();
        blur_panel = new javax.swing.JPanel();
        blur_value = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();

        addWindowListener(new java.awt.event.WindowAdapter() {

            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        ok_cancel.setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints1;

        ok.setText("OK");
        ok.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        gridBagConstraints1 = new java.awt.GridBagConstraints();
        ok_cancel.add(ok, gridBagConstraints1);

        cancel.setText("CANCEL");
        cancel.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = 1;
        ok_cancel.add(cancel, gridBagConstraints1);

        getContentPane().add(ok_cancel, java.awt.BorderLayout.EAST);

        center.setLayout(new java.awt.BorderLayout());

        blur_panel.setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints2;

        blur_panel.setBorder(new javax.swing.border.TitledBorder("BLUR"));
        blur_value.setColumns(10);
        blur_value.setText("1");
        blur_value.setMaximumSize(new java.awt.Dimension(1, 50));
        blur_value.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                blurActionPerformed(evt);
            }
        });

        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.gridx = 1;
        gridBagConstraints2.gridy = 1;
        blur_panel.add(blur_value, gridBagConstraints2);

        jLabel4.setText("blur");
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.gridx = 0;
        gridBagConstraints2.gridy = 1;
        blur_panel.add(jLabel4, gridBagConstraints2);

        jLabel7.setText("times");
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.gridx = 2;
        gridBagConstraints2.gridy = 1;
        blur_panel.add(jLabel7, gridBagConstraints2);

        center.add(blur_panel, java.awt.BorderLayout.CENTER);

        getContentPane().add(center, java.awt.BorderLayout.CENTER);
        /*FAULT::jLabel4.setText("notblur");*/
        pack();
    }

    /** This method executes if the event is a CANCEL button, then it closes the dialog.
     * There are no OS dependencies and variances.  No security constraints or external
     * specifications.
     * @param evt The event of a mouse action.
     */
    public void cancelActionPerformed(java.awt.event.ActionEvent evt) {
        this.closeDialog(new java.awt.event.WindowEvent(this, 0));
    }

    /** This method executes if the event is a OK button.  Then it executes the performed action.
     * In this case, it will blur the picture
     * accordingly.  If user selected skew instead of blur, the picture will
     * basically just expand without the borders of the picture to expand.
     *
     * There are no state transitions and no security constraints or OS dependencies.
     *
     * @param evt The event of a mouse action.
     */
    public void okActionPerformed(java.awt.event.ActionEvent evt) {
        // ming 4.24
        int cur_layer = ((Paint) this.getParent()).center.currentLayer;
        LinkedList cur_action_list = (LinkedList) (((Paint) this.getParent()).action_list.get(cur_layer));
        LinkedList cur_redo_list = (LinkedList) (((Paint) this.getParent()).redo_list.get(cur_layer));

        int temp1 = cur_redo_list.size();
        for (int i = 0; i < temp1; i++) {
            cur_redo_list.removeLast();
        }
        if (((Paint) this.getParent()).toolSelectall.getSelected() || ((Paint) this.getParent()).toolSelect.getSelected() || ((Paint) this.getParent()).toolMagicSelect.getSelected()) {
            cur_action_list.add("Region blur");
            BufferedImage selected;
            if (((Paint) this.getParent()).currentTool == ((Paint) this.getParent()).toolSelect) {
                selected = ((Paint) this.getParent()).toolSelect.getCopyImage(((Paint) this.getParent()).center);
            } else if (((Paint) this.getParent()).currentTool == ((Paint) this.getParent()).toolMagicSelect) {
                selected = ((Paint) this.getParent()).toolMagicSelect.getCopyImage(((Paint) this.getParent()).center);
            } else {
                selected = ((Paint) this.getParent()).toolSelectall.getCopyImage(((Paint) this.getParent()).center);
            }
            int x = Integer.parseInt(blur_value.getText());

            Kernel kernel = new Kernel(3, 3, new float[]{1f / 9f, 1f / 9f,
                        1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f,
                        1f / 9f
                    });
            RenderingHints hints = new RenderingHints(
                    RenderingHints.KEY_RENDERING,
                    RenderingHints.VALUE_RENDER_QUALITY);
            BufferedImageOp op = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,
                    hints);

            for (int i = 0; i < x; i++) {
                selected = op.filter(selected, null);
            }
            if (((Paint) this.getParent()).currentTool == ((Paint) this.getParent()).toolSelect) {
                ((Paint) this.getParent()).toolSelect.setPastedImage(
                        selected, ((Paint) this.getParent()).center,
                        ((Paint) this.getParent()).toolSelect.getStartX(),
                        ((Paint) this.getParent()).toolSelect.getStartY());
            } else if (((Paint) this.getParent()).currentTool == ((Paint) this.getParent()).toolSelectall) {
                ((Paint) this.getParent()).toolSelectall.setPastedImage(
                        selected, ((Paint) this.getParent()).center,
                        ((Paint) this.getParent()).toolSelectall.getStartX(),
                        ((Paint) this.getParent()).toolSelectall.getStartY());
            } else if (((Paint) this.getParent()).currentTool == ((Paint) this.getParent()).toolSelect) {
                ((Paint) this.getParent()).toolSelect.setPastedImage(
                        selected, ((Paint) this.getParent()).center,
                        ((Paint) this.getParent()).toolSelect.getStartX(),
                        ((Paint) this.getParent()).toolSelect.getStartY());
            //BufferedImage curImage = ((Paint)this.getParent()).center.getBufferedImage();
            //((Paint)this.getParent()).center.setBufferedImage(selected);//curImage);
            //((Paint)this.getParent()).center.repaint();
            }
            selected.flush();
        } else {
            cur_action_list.add("Image blur");
            // ming 4.24 end
            BufferedImage im = ((Paint) this.getParent()).center.getBufferedImage();

            int x = Integer.parseInt(blur_value.getText());

            Kernel kernel = new Kernel(3, 3, new float[]{1f / 9f, 1f / 9f,
                        1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f,
                        1f / 9f
                    });
            // ming 4.24
            RenderingHints hints = new RenderingHints(
                    RenderingHints.KEY_RENDERING,
                    RenderingHints.VALUE_RENDER_QUALITY);
            BufferedImageOp op = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,
                    hints);
            // ming 4.24 end

            for (int i = 0; i < x; i++) {
                im = op.filter(im, null);
            }
            Graphics2D gd = im.createGraphics();

            gd.drawImage(im, 0, 0, null);
            ((Paint) this.getParent()).center.setBufferedImage(im);
            // ming 4.24
            im.flush();
        }
        // ming 4.24 end
        // ming 4.26

        // ming 4.26 end

        ((Paint) this.getParent()).updateUndoList();

        this.closeDialog(new java.awt.event.WindowEvent(this, 0));
    //	  ok_action = true;
    }

    /** This method is for blurring.
     * There are no OS dependencies and variances.  No security constraints or external
     * specifications.
     * @param evt The event of an action.
     */
    public void blurActionPerformed(java.awt.event.ActionEvent evt) {
        // Add your handling code here:
    }

    /** Closes the dialog.
     * There are no OS dependencies and variances.  No security constraints or external
     * specifications.
     * @param evt The event of an action.
     */
    public void closeDialog(java.awt.event.WindowEvent evt) {
        setVisible(false);
        dispose();
    /*FAULT::setVisible(true);*/
    }

    /** This method basically creates a new blur object and applies the image
     * into it.	 If there are arguments, it will take them as well.  It will then
     * display the results onto the main canvas.
     * There are no OS dependencies and variances.  No security constraints or external
     * specifications.
     * @param args The command line arguments
     */
    public static void main(String args[]) {
        new blur(new javax.swing.JFrame(), true).show();
    }

    // Variables declaration - do not modify
    /** Java swing GUI depicting the OK and CANCEL panel.
     */
    public javax.swing.JPanel ok_cancel;
    /** Java swing GUI depicting the OK button.
     */
    public javax.swing.JButton ok;
    /** Java swing GUI depicting the CANCEL button.
     */
    public javax.swing.JButton cancel;
    /** Java swing GUI depicting the CENTER button.
     */
    private javax.swing.JPanel center;
    /** Java swing GUI depicting the blur button.
     */
    private javax.swing.JPanel blur_panel;
    /** Java swing GUI depicting the textfield of the blur change ratio value.
     */
    private javax.swing.JTextField blur_value;
    /** Java swing GUI depicting the Label 4.
     *
     */
    public javax.swing.JLabel jLabel4;
    /** Java swing GUI depicting the Label 7.
     *
     */
    private javax.swing.JLabel jLabel7;

    // End of variables declaration
    /** The dialog is closed by OK
     */
    // public boolean ok_action;
}